<template>
  <!-- 注册页面整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box">管理员注册</div>
      <!-- 注册表单区域 -->
      <el-form ref="form" :model="form" :rules="rulesObj">
        <el-form-item props="username">
          <el-input placeholder="请输入用户名" v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item props="password">
          <el-input type="password" placeholder="请输入密码" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item  props="repassword">
          <el-input type="password" placeholder="请确认密码" v-model="form.repassword"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="btn-reg" type="primary" @click="registerFn">注册</el-button>
          <el-link type="info" @click="$router.push('/adminlogin')">去登陆</el-link>
        </el-form-item>

      </el-form>
    </div>
  </div>
</template>

<script>
import { registerAPI } from '@/api/login'
export default {
  name: 'my-register',

  data () {
    const samePwdFn = (rule, value, callback) => {
      if (value !== this.form.password) {
        // 如果验证失败，则调用回调函数时，指定一个Error对象
        callback(new Error('两次输入的密码不一致！'))
      } else {
        // 验证成功，则调用callback回调函数即可
        callback()
      }
    }
    return {
      form: {
        username: '',
        password: '',
        repassword: ''
      },
      rulesObj: { // 表单校验规则对象
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '用户名必须是1-10的大小写字母数字', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是1-10的大小写字母数字', trigger: 'blur' }
        ],
        repassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码必须是1-10的大小写字母数字', trigger: 'blur' },
          { validator: samePwdFn, trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    // 注册的点击事件
    registerFn () {
      // js兜底表单校验
      this.$refs.form.validate(async valid => {
        if (valid) {
          // 通过校验,拿到绑定的数据 调用注册接口
          const { data: res } = await registerAPI(this.form)
          console.log(res)
          // 注册失败，提示用户
          // ElementUI在vue原型链上添加了弹窗提示
          if (res.code !== 0) return this.$message.error(res.msg)
          // 注册成功，提示用户
          this.$message.success(res.msg)
          // 跳转到登陆页面
          this.$router.push('/login')
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 800px;

  .reg-box {
    width: 400px;
    height: 330px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    box-sizing: border-box;

    .title-box {
      height: 60px;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .btn-reg {
      width: 100%;
    }
  }
}
</style>
